<!DOCTYPE html>
<html>
    <head>
        <title>Initialization Data not returned when using different origin</title>
        <script src="/js-test-resources/testharness.js"></script>
        <script src="/js-test-resources/testharnessreport.js"></script>
    </head>
    <body>
        <video id="testVideo"></video>
        <div id="log"></div>
        <script>
            async_test(function(test)
            {
                var video = document.getElementById('testVideo');
                var encryptedEventCount = 0;

                function logMessage(msg) {
                    var date = new Date();
                    var hours = ('0' + date.getHours()).slice(-2);
                    var minutes = ('0' + date.getMinutes()).slice(-2);
                    var secs = ('0' + date.getSeconds()).slice(-2);
                    var milliSecs = ('00' + date.getMilliseconds()).slice(-3);
                    console.log(hours + ':' + minutes + ':' + secs + '.' + milliSecs + ': ' + msg);
                };

                function onEncrypted(event)
                {
                    logMessage('onEncrypted');
                    assert_equals(event.target, video);
                    assert_true(event instanceof window.MediaEncryptedEvent);
                    assert_equals(event.type, 'encrypted');

                    // Since the .src is in a different origin as this test,
                    // both initDataType and initData should be empty.
                    assert_equals(event.initDataType, '');
                    assert_equals(event.initData.byteLength, 0);

                    // Both the audio and the video tracks have initData,
                    // so finish once both events are received.
                    if (++encryptedEventCount == 2) {
                        logMessage('done');
                        test.done();
                    }
                }

                logMessage('starting');
                navigator.requestMediaKeySystemAccess(
                    'org.w3.clearkey',
                    [{audioCapabilities: [{contentType: 'audio/webm; codecs=vorbis'}]}])
                  .then(function(access) {
                    return access.createMediaKeys();
                }).then(function(mediaKeys) {
                    video.addEventListener('encrypted', test.step_func(onEncrypted), true);
                    return video.setMediaKeys(mediaKeys);
                }).then(function(result) {
                    // Verify that the current origin is different than what
                    // we use for the content.
                    logMessage('setting src');
                    assert_equals(self.origin, 'http://127.0.0.1:8000');
                    video.src = 'http://127.0.0.1:8080/media/encrypted-media/test-encrypted.webm';
                    video.play();
                });
            }, 'Initialization Data not returned when using different origin.');
        </script>
    </body>
</html>
